package elitech.vietnam.copyweatherfashion.adapters;

import java.io.IOException;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.badge.BadgeHelper;
import elitech.vietnam.copyweatherfashion.config.Const;
import elitech.vietnam.copyweatherfashion.entities.Notify;
import elitech.vietnam.copyweatherfashion.fragments.MemberPostFragment;
import elitech.vietnam.copyweatherfashion.fragments.settings.FriendListFragment;
import elitech.vietnam.copyweatherfashion.gcm.GCMIntentService.GCMEntity;
import elitech.vietnam.copyweatherfashion.utilities.TimeDiff;
import elitech.vietnam.copyweatherfashion.wsclient.Service;

public class NotifyArrayAdapter extends ArrayAdapter<Notify> {
	
	LayoutParams mLParam;
	MainActivity mActivity;
	
	public NotifyArrayAdapter(Context context, int resource, List<Notify> objects) {
		super(context, resource, objects);
		mLParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		mActivity = (MainActivity) getContext();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder holder;
		
		if (row == null) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			row = inflater.inflate(R.layout.notification_list_item, parent, false);
			
			holder = new ViewHolder();
			holder.image = (ImageView) row.findViewById(R.id.notiitem_image);
			holder.title = (TextView) row.findViewById(R.id.notiitem_content);
			holder.time = (TextView) row.findViewById(R.id.notiitem_time);

			row.setTag(holder);
		} else
			holder = (ViewHolder) row.getTag();
		
		final Notify noti = getItem(position);
		GCMEntity e = GCMEntity.newInstance(getContext(), noti);
		holder.title.setText(Html.fromHtml(e.bigtext));
		UrlImageViewHelper.setUrlDrawable(holder.image, Const.SERVER_IMAGE_URL + noti.Image, R.drawable.avatar);
		holder.time.setText(TimeDiff.getTimeDiffString(noti.Date(), mActivity.mClock, getContext()));
		if (noti.Unread())
			row.setBackgroundResource(R.drawable.item_notification_unread);
		else
			row.setBackgroundResource(R.drawable.item_notification);
		
		row.setLayoutParams(mLParam);
		
		row.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mActivity.mNotiWindow.dismiss();
				if (noti.isNew) {
					mActivity.mBadgeCount -= 1;
					mActivity.notifyBadgeChanged();
					noti.isNew = false;
					BadgeHelper.updateBadge(mActivity, mActivity.mBadgeCount);
				}
				noti.Unread = 0;
				new SetNotificationUnread().execute(noti.Id);
				if (noti.Type == 1)
					mActivity.getCurrentBase().replaceFragment(MemberPostFragment.newInstance(noti.PostID), true);
				if (noti.Type == 2)
					mActivity.getCurrentBase().replaceFragment(MemberPostFragment.newInstance(noti.PostID), true);
				if (noti.Type == 3)
					mActivity.getCurrentBase().replaceFragment(MemberPostFragment.newInstance(noti.PostID), true);
				if (noti.Type == 4) {
					FriendListFragment fragment = new FriendListFragment();
					Bundle bundle = new Bundle();
					bundle.putInt(FriendListFragment.ARG_RESUMETAB, 1);
					fragment.setArguments(bundle);
					mActivity.getCurrentBase().replaceFragment(fragment, true);
				}
			}
		});
		
		return row;
	}
	
	static class ViewHolder {
		ImageView image;
		TextView title, time;
	}

	static class SetNotificationUnread extends AsyncTask<Integer, Integer, Boolean> {
		@Override
		protected Boolean doInBackground(Integer... params) {
			try {
				new Service().readNotification(params[0]);
			} catch (IOException | XmlPullParserException e) {
				e.printStackTrace();
			}
			return null;
		}
	}
}
